// 2025/9/19
// 单词规律

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<char, string> phash;
        unordered_map<string, char> shash;
        int m = pattern.size(), n = s.size();
        int i = 0, j = 0;
        for(; i < m && j < n; i++)
        {
            int pos = s.find(" ", j);
            string tmp;
            if(pos != string::npos)
            {
                tmp = s.substr(j, pos - j);
                j = pos + 1;
            }
            else
            {
                tmp = s.substr(j);
                j = n;
            }
            
            if(phash.count(pattern[i]) && phash[pattern[i]] != tmp)
                return false;
            if(shash.count(tmp) && shash[tmp] != pattern[i])
                return false;
            phash[pattern[i]] = tmp;
            shash[tmp] = pattern[i];
        }
        return (i == m && j == n);
    }
};